//
//  MXSNetworkTools.swift
//  weibo
//
//  Created by 孙飞 on 15/12/9.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import AFNetworking

enum MXSRequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class MXSNetworkTools: AFHTTPSessionManager {
    
    
    // 给通过的返回 闭包 定义一个别名
    typealias MXSNetworkToolsCallBack = (response: AnyObject?, error: NSError?)->()
    
    static let shareTools: MXSNetworkTools = {
        
        
        
        let tools = MXSNetworkTools()
        // 在 Swift 里面, 会把 NSSet 转成 Set
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    /// 发送请求
    ///
    /// - parameter method:    请求方式
    /// - parameter urlString: 请求地址
    /// - parameter params:    请求参数
    /// - parameter callback:  请求成功之后的回调
    
    func request(method: MXSRequestMethod, urlString: String, params: AnyObject?, callback: MXSNetworkToolsCallBack){
        
        // 在 Swift 中,是不能向当前类(AFHTTPSessionManager)发送 dataTaskWithHTTPMethod
        // 因为 dataTaskWithHTTPMethod 这个方法是 AFN 写到 .m 文件里面
        // 那么对应在 Swift 里面就相当于是 private 修饰的方法
        
        // 定义请求成功之后的闭包
        let succCallback = { (dataTask: NSURLSessionDataTask, response: AnyObject) -> Void in
            callback(response: response, error: nil)
        }
        
        // 定义请求失败的闭包
        let failureCallBack = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            callback(response: nil, error: error)
        }
        
        // 根据不同的请求类型,调用不同的请求方法
        if method == .GET {
            self.GET(urlString, parameters: params, success: succCallback, failure: failureCallBack)
        }else{
            self.POST(urlString, parameters: params, success: succCallback, failure: failureCallBack)
        }
    }
}


//// MARK: - 首页微博数据请求
//extension MXSNetworkTools {
//    
//    func loadStatus(accessToken: String, callback: HMNetworkToolsCallBack) {
//        // 请求地址
//        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
//        // 请求参数
//        let params = [
//            "access_token": accessToken
//        ]
//        request(.GET, urlString: urlString, params: params, callback: callback)
//    }
//    
//}

// MARK: - 首页微博数据请求
extension MXSNetworkTools {
    
    func loadStatus(accessToken: String,sinceId: Int64 = 0, maxid:Int64 = 0, callback: MXSNetworkToolsCallBack) {
        // 请求地址
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        // 请求参数
        let params = [
            "access_token": accessToken,
            "max_id": "\(maxid)",
            "since_id": "\(sinceId)"
        ]
        request(.GET, urlString: urlString, params: params, callback: callback)
    }
    
}


extension MXSNetworkTools {
    
    // 加载accessToken,并且把加载完成之后的数据回调回去
    func loadAccessToken(code: String, callback: MXSNetworkToolsCallBack){
        
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": WB_APPKEY,
            "client_secret": WB_APP_SECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WB_REDIRECT_URI
        ]
        
        request(.POST, urlString: urlString, params: params, callback: callback)
    }
    
    
    //请求用户信息
    func loadUserInfo(uid: String, accessToken: String, callback: MXSNetworkToolsCallBack){
        // 定义 url
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 组织参数
        let params = [
            "access_token": accessToken,
            "uid": uid
        ]
        // 发送请求
        request(.GET, urlString: urlString, params: params, callback: callback)
    }
}
